<?php

namespace App\Http\Controllers\Admin;
use DB;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class TopicController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //话题管理首页
    public function getIndex(Request $request)
    {
        $val = $request->input("search");
        //便利出所有的话题,并在前台显示
        $res = self::cate(0);
        if(!$val)
        {
            $topics = DB::table("topic")->select(DB::raw("*,concat(path,',',id) as paths "))->orderBy('paths')->paginate(10);
        }
        else
        {
            $topics = DB::table("topic")->select(DB::raw("*,concat(path,',',id) as paths "))
            ->where("topic_name","like","%".$val."%")
            ->orderBy('paths')->paginate(10);
        }
        return view("Admin.topic.index",["topics"=>$topics,"val"=>$val,"res"=>$res]);
    }
    public static function cate($id)
    {
        $arr = array();
        $res = DB::table("topic")->where("pid",$id)->get();
        foreach($res as $k=>$v)
        {
            $v->sub = self::cate($v->id);
            $arr[] = $v;
        }
        return $arr;
    }
    //增加话题
     public function getAdd(Request $request)
    {
        //判断是否是从父类的增加处点进来
        $fid = $request->input("fid","");
        //便利出所有话题
        $res = DB::select("select *,concat(path,',',id) as paths from topic order by paths");
        foreach ($res as $key => $value) {
            $arr = explode(",",$value->path);
            $count = count($arr);
            //拼接字符串
            $value->topic_name = str_repeat("--|",$count-1).$value->topic_name;
        }
        return view("Admin.topic.add",["res"=>$res,"fid"=>$fid]);
    }

    public function postInsert(Request $request)
    {
        if($request->hasFile("topic_portrait"))
        {
            $arr = array("jpg","png","jpeg","gif");
            $suffix = $request->file("topic_portrait")->getClientOriginalExtension();
            if(in_array($suffix,$arr))
            {
                $name = md5(rand(100,1000).time());
                $request->file("topic_portrait")->move("./upload",$name.".".$suffix);
            }
        }
        else
        {
            $request->flash();
            return back()->with("error","话题图标未上传");
        }
        $res = $request->only(["pid","topic_name","topic_summary"]);
        $res["topic_portrait"] = "/upload/".$name.".".$suffix;
        $topic_name = $request->input("topic_name");
        $pid = $request->input("pid");
        if($pid)
        {
            $res1 = DB::table("topic")->where("id",$pid)->first();
            $res["path"] = $res1->path.",".$res1->id;
        }
        else
        {
            $res["path"] = 0;
        }
        $val = DB::table("topic")->insert($res);
        if($val)
        {
            if($res["pid"] == 0)
            {
                return redirect("/admin/topic/add")->with("success","顶级分类增加成功,您可以为该父话题继续增加子话题");
            }
            else
            {
                return redirect("/admin/topic/index")->with("success","话题增加成功");
            }
        }
        else
        {
            return back()->with("error","话题增加失败");
        }
    }
    //显示修改话题的页面
    public function getEdit($id)
    {
        //查询话题
        $res = DB::table("topic")->where("id",$id)->first();
        //查询是否修改的为父话题
        if($res->pid != 0)
        {
            //修改的是子话题,则查出其父话题
            $res1 = DB::table("topic")->where("id",$res->pid)->first();
            //解析
            return view("Admin.topic.edit",['res'=>$res,"res1"=>$res1]);
        }
        else
        {
            //修改的是父话题,将父话题数据返回
            return view("Admin.topic.edit",['res'=>$res]);
        }
    }
    //提交修改数据
    public function postUpdate(Request $request)
    {
        //提取需要更改的信息
        $res = $request->only(["id","topic_name","topic_summary"]);
        $id = $request->input("id");
        //判断是否更改了图片
        if($request->hasFile("topic_portrait"))
        {
            $arr = array("jpg","png","jpeg","gif");
            $suffix = $request->file("topic_portrait")->getClientOriginalExtension();
            if(in_array($suffix,$arr))
            {
                $name = md5(rand(100,1000).time());
                $request->file("topic_portrait")->move("./upload",$name.".".$suffix);
                //图标修改成功,将图片存入数组
                $res["topic_portrait"] = "/upload/".$name.".".$suffix;
            }
            else
            {
                $request->flash();
                return back()->with("error","话题图标不合要求");
            }
        }
        //执行话题修改
        $val = DB::table("topic")->where("id",$id)->update($res);
        if($val)
        {
            return redirect("/admin/topic/index")->with("success","话题修改成功");
        }
        else
        {
            return back()->with("error","话题修改失败,请联系程序员哥哥");
        }
    }
    //删除话题 当话题下没有问题的时候,这个话题就可以删除
    public static function check($id)
    {
        //根据传过来的id判断是父级话题还是子集话题
        $val = DB::table("topic")->where("id",$id)->first();
        $pid = $val->pid;
        //如果是父级话题,判断父级话题下是否有子级话题
        if($pid == 0)
        {
            $count = DB::table("topic")->where("pid",$id)->get();
            if($count)
            {
                //不能删除父话题
                return false;
            }
            else
            {
                //可以删除父话题
                return true;
            }
        }
        //如果是子集话题,就判断该子集话题下是否有问题
        else
        {
        //查询当前子话题下是否有问题
           $res = DB::table('topic_question')
                    ->select(DB::raw('count(*) as question_count'))
                    ->where('topic_id',$id)
                    ->get();
            if($res[0]->question_count>0)
            {
                //不能删除子话题
                return false;
            }
            else
            {
                //可以删除子话题
                return true;
            }
        }
    }
    //删除子话题
    public function getDelete($id)
    {
        $res = DB::table("topic")->where("id",$id)->delete();
        if ($res) {
            return back()->with("success","话题删除成功");
        }
        else
        {
            return back()->with("error","话题删除失败,请联系程序员哥哥");
        }
    }

}
 
